<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace crcl\base\template\container\xhtml\instructions;

interface IIf
{
}

class ContainerIf extends Concrete implements IIf, IConcrete
{
  public function start()
  {
    $oTrigger = new \crcl\base\template\Trigger();
    if ($this->hasAttribute('attribute'))
    {
      $oTrigger->setScope(\crcl\base\template\container\xhtml\Instruction::SCOPE_NEXT_ATTRIBUTE_REPLACE);
    }
    else
    {
      $oTrigger->setScope(\crcl\base\template\container\xhtml\Instruction::SCOPE_CURRENT_CLOSE);
    }

    $oTrigger->setInstruction($this);

    return $oTrigger;
  }

  public function executeCurrent()
  {
    if (!$this->hasAttribute('attribute'))
    {

      $var = $this->getAttribute('if');
      $var = \substr($var, 1); //remove trailing $

      $sOutput = self::endHeredoc() . 'if ($oValues->getFalse(\'' . $var . '\') ';
      if ($this->hasAttribute('equals'))
      {
        $sEquals = $this->getAttribute('equals');
        if (strlen($sEquals)>0 && $sEquals{0} !== '$')
        {
          $sOutput .= '== \'' . $sEquals . '\') { ' . self::startHeredoc();
        }
        elseif ($sEquals == '')
        {
          $sOutput .= '== \'\') { ' . self::startHeredoc();
        }
        else
        {
          $sEquals = \substr($sEquals, 1); //remove trailing $
          $sOutput .= '== $oValues->get(\'' . $sEquals . '\')) { ' . self::startHeredoc();
        }
      }
      elseif ($this->hasAttribute('notequals'))
      {
        $sEquals = $this->getAttribute('notequals');
        if (strlen($sEquals)>0 && $sEquals{0} !== '$')
        {
          $sOutput .= '!= \'' . $sEquals . '\') { ' . self::startHeredoc();
        }
        elseif ($sEquals == '')
        {
          $sOutput .= '!= \'\') { ' . self::startHeredoc();
        }
        else
        {
          $sEquals = \substr($sEquals, 1); //remove trailing $
          $sOutput .= '!= $oValues->get(\'' . $sEquals . '\')) { ' . self::startHeredoc();
        }
      }

      else
      {
        $sOutput .= '== \TRUE) { ' . self::startHeredoc();
      }

      $this->sContent = $sOutput;
      $this->sContentClose = self::endHeredoc() . '} ' . self::startHeredoc();
    }
  }

  public function executeNext($sTag, $aAttributes)
  {
    $this->aContentAttributes = $aAttributes;

    if ($this->hasAttribute('attribute') && ($this->hasAttribute('content')))
    {
      $sAttribute = $this->getAttribute('attribute');
      unset($aAttributes[$sAttribute]);
      $this->aContentAttributes = $aAttributes;

      $var = $this->getAttribute('if');
      $var = \substr($var, 1); //remove trailing $

      $sOutputPre = self::endHeredoc() . 'if ($oValues->getFalse(\'' . $var . '\') ';
      if ($this->hasAttribute('equals'))
      {
        $sEquals = $this->getAttribute('equals');
        if ((strlen($sEquals)>0) && ($sEquals{0} !== '$'))
        {
          $sOutputPre .= '== $oValues->out(\'' . $sEquals . '\')) { ';
        }
        elseif ($sEquals == '')
        {
          $sOutputPre .= '== \'\') { ';
        }
        else
        {
          $sEquals = \substr($sEquals, 1); //remove trailing $
          $sOutputPre .= '== $oValues->get(\'' . $sEquals . '\')) { ';
        }
      }
      else
      {
        $sOutputPre .= '== \TRUE) { ';
      }
      $sOutputPre .= self::startHeredoc() . ' ';

      if (\strpos($this->getAttribute('content'), '$') === \FALSE)
      {
        $sOutputPost =// self::startHeredoc() .
          '"' . $this->getAttribute('content') . '"' .
                    self::endHeredoc(); //totally fixed
      }
      else
      {
        //dynamic and mixed
        $sTerm = \str_replace('\'', '\\\'', $this->getAttribute('content'));
        $sOutputPost = '$oValues->out(\'' . $sTerm . '\')';
      }
      $sOutputPost .= '} %NO-QUOTES%' . self::startHeredoc();
      $this->aContentAttributes[$sOutputPre . $this->getAttribute('attribute')] = $sOutputPost;
    }
  }

  /** PRIVATE *************************************************************************************/

  protected $aAllowedAttributes = array('if', 'equals', 'notequals', 'attribute', 'content');
}